package com.jy.adscm.operation.mscyb.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqdbSettings;
import com.jy.adscm.operation.mscyb.service.JqdbSettingsService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-06-17
 */
@RestController
@RequestMapping("/adscm/operation/jqdb/jqdbSettings")
public class JqdbSettingsController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
   JqdbSettingsService JqdbSettingsService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqdbSettings jqdbSettings, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqdbSettings.setCreator(token.getUser().getId().toString());
        jqdbSettings.setCreateTime(new Date());
        jqdbSettings.setIsEnable(1);
        boolean insert = JqdbSettingsService.insert(jqdbSettings);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqdbSettings jqdbSettings, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqdbSettings.setCreator(token.getUser().getId().toString());
        jqdbSettings.setCreateTime(new Date());
        boolean result = JqdbSettingsService.updateById(jqdbSettings);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectById/{id}")
    public  SimpleCoreResponse  selectByTid(@PathVariable String id) throws  AdscmSystemException{
        JqdbSettings JqdbSettings = JqdbSettingsService.selectJqdbSettingsById(id);
        return  new SimpleCoreResponse("200","查询成功",JqdbSettings);
    }
    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll( HttpServletRequest request) throws  AdscmSystemException{
        List<JqdbSettings> JqdbSettingss =  JqdbSettingsService.selectAll();

        return  new SimpleCoreResponse("200","查询成功",JqdbSettingss);
    }
}

